#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::istringstream;

void test0(){
    // 开始就将文件游标放在结尾位置
    ifstream ifs("01_stream.cc", std::ios::ate);
    if (!ifs){
        cerr << "ifstream open file failed"  << endl;
        return;
    }

    size_t size = ifs.tellg();
    ifs.seekg(0);

    char * data = new char[size + 1]();
    ifs.read(data, size);
    // cout << data << endl;
    
    string str1(data);

    delete[] data;
    data = nullptr;
    
    cout << str1 << endl;

    ifs.close();
}

void test1(){
    ifstream ifs("01_stream.cc");
    if (!ifs){
        cerr << "ifstream open file failed"  << endl;
        return;
    }

    ifs.seekg(0, std::ios::end);
    long length = ifs.tellg();
    cout << length << endl;

    ifs.seekg(0);

    char * data = new char[length]();
    ifs.read(data, length - 1);

    string content(data);
    cout << "content: " << content << endl;

    delete[] data;
    data = nullptr;
    ifs.close();
}

void test2(){
    ofstream ofs("test.txt", std::ios::app);
    string line("hello world\n");
    
    ofs << line;
    ofs << line;
    ofs << line;
}

void test3(){
    string s("123 456");
    istringstream iss(s);

    int num1 = 0;
    int num2 = 0;

    iss >> num1 >> num2;
    cout << "num1 = " << num1 << 
        " num2 = " << num2 << endl;
}

int main(void){
    test0();
    return 0;
}

